﻿import QtQuick 2.5
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4
import QtQuick.Controls.Private 1.0

Item {
    id: item
    width: 300
    height: 400
//    visible: false
    anchors.centerIn: parent

    property real rx: 300
    property real ry: 300
    property real rr: 200
    property real lx: 400
    property real ly: 50
    property real lspacing: 30
    property int listLength: 0
    property var listPoint: new Array

    Rectangle{
        id: rectBg
        anchors.fill: parent
        color: "lightgreen"
        opacity: 0.5
    }

    function setVisible(vis){
        item.visible = vis
    }

    //设置列表数据
    //map.value 数量
    //map.text 名称
    function setList(list){
        for(var i=0; i<list.length; i++){
            console.log(list.length, list[i].text, list[i].value)
            listModel.append({"name1": list[i].text, "value": list[i].value})
        }
        setVisible(true)
    }

    function setLis1t(list){
        var sum = 0
        var spacingNum = 0
        var spacing = 0
        listLength = list.length
        listPoint.length = 0
        for(var i in list){
            sum += list[i].value
        }
        if(list.length > 1){
            spacingNum = list.length
        }

        var all360 = 360 + spacingNum * spacing

        var lastAngle = 0

        //clear
        var ctx = canvas.getContext('2d')
        ctx.clearRect(0, 0, canvas.width, canvas.height)

        for(var i in list){

            Math.seed = getSeed()

            var color = Qt.rgba(Math.random()+0.2,Math.random()+0.2,Math.random()+0.2,1)


            var size = list[i].value / sum *  360  / all360 * 360
            getPath(Math.PI/180*lastAngle,Math.PI/180*(size+lastAngle),color,list[i].text+" X"+list[i].value)
//            addLegend(i,color,list[i].text+" "+list[i].value+"个")
            lastAngle += (size + spacing)
        }

        drawText()

        setVisible(true)
    }

    //设置查询区域
    function setQueryArea(list){
        txtQuery.text = qsTr("查询区域:") + "\n"

        for(var i in list){
            txtQuery.text += list[i] + "\n"
        }
    }

    /** 扇形
      * @param x 扇形圆心x左标
      * @param y 扇形圆心y坐标
      * @param r 扇形半径
      * @param angleBegin 扇形开始角度
      * @param angleEnd 扇形结束角度
      * @param ifAnticlockwise 是否是逆时针
      */

    function getPath(angleBegin,angleEnd,color,text){
        var ctx = canvas.getContext('2d')
        ctx.beginPath()
        ctx.fillStyle = color
        ctx.strokeStyle = color
        ctx.lineWidth = 100
        ctx.arc(rx,ry,rr,angleBegin,angleEnd,false)
        ctx.stroke()
        ctx.closePath()

        ctx.beginPath()
        //点坐标
        var x1 = rx + (rr)*Math.cos(angleBegin+(angleEnd-angleBegin)/2)
        var y1 = rx + (rr)*Math.sin(angleBegin+(angleEnd-angleBegin)/2)
        var map = {}
        map.point = Qt.point(x1,y1)
        map.text = text
        listPoint.push(map)

//        ctx.arc(x1,y1,5,0,2*Math.PI)
//        ctx.fillStyle = "red"
//        ctx.fill()

        ctx.closePath()
        canvas.requestPaint()
    }

    function drawText(){
        var ctx = canvas.getContext('2d')
        ctx.beginPath()
        for(var j in listPoint){
            var fontsize = 10

            ctx.font=""+fontsize+"px Arial"
            ctx.textAlign="center"
            ctx.fillStyle = "black"
            ctx.fillText(listPoint[j].text,listPoint[j].point.x,listPoint[j].point.y)
            ctx.restore()
        }
        ctx.closePath()
        canvas.requestPaint()
    }

    //图例
    function addLegend(index,color,text){
        var ctx = canvas.getContext('2d')
        ctx.beginPath()

        ctx.fillStyle = color
        ctx.fillRect(lx,ly + lspacing*index,20,20)

        ctx.font="20px Arial"
        ctx.textAlign="start"
        ctx.fillText(text,lx + 30,ly + lspacing*index+17)

        ctx.closePath()
        canvas.requestPaint()
    }

    function addZero(value){
        if(value < 10){
            return "0" + value
        }
        return value
    }

    function getSeed(){

        var date = new Date
        var y = date.getFullYear()
        var M = addZero(date.getMonth())
        var d = addZero(date.getDate())
        var h = addZero(date.getHours())
        var m = addZero(date.getMinutes())
        var s = addZero(date.getSeconds())

        var time = String(y) + String(M) + String(d) + String(h) + String(m) + String(s)

        return Number(time)
    }

    Canvas{
        id: canvas
        width: 600
        height: 600
        visible: false
        onPaint: {

        }
    }

    //查询区域
    Text {
        id: txtQuery
        x: 600
        y: 20
        visible: false
        width: 100
        height: parent.height - 40
        elide: Text.ElideRight
        font.family: "微软雅黑"
        font.pixelSize: 20
        color: "white"
    }

    Component{
        id: delegate
        Rectangle{
            width: listView.width
            height: 16
            color: "white"

            Text{
                id: text
                width: listView.width
                elide: Text.ElideRight
                horizontalAlignment: Text.AlignHCenter
                height: 16
                color: "black";
                font.pixelSize: 16;
                smooth: true;
                text: name1 + ":" + value
            }
        }

    }



}
